package com.fmsz.physicalheadline.fragments;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.application.MyApplication;
import com.fmsz.physicalheadline.databinding.FragmentBaseNewsBinding;

import com.fmsz.physicalheadline.adapter.news.NewsPagerAdapter;
import com.fmsz.physicalheadline.entity.news.NewsTagEntity;
import com.fmsz.physicalheadline.interfaces.HttpService;
import com.fmsz.physicalheadline.interfaces.SlidingOpen;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseNewsFragment extends Fragment implements Callback<NewsTagEntity> {

    private FragmentBaseNewsBinding binding;
    private List<NewsTagEntity.NewsTagData.ChannelsEntity> list;
    private NewsPagerAdapter pagerAdapter;
    private SlidingOpen slidingOpen;

public static BaseNewsFragment newInstance() {

    Bundle args = new Bundle();

    BaseNewsFragment fragment = new BaseNewsFragment();
    fragment.setArguments(args);
    return fragment;
}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        slidingOpen = (SlidingOpen) getActivity();
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_base_news, container, false).getRoot();
        binding = DataBindingUtil.bind(view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        HttpService httpService = ((MyApplication) getActivity().getApplication()).getHttpService();
        httpService.getNewsTag().enqueue(this);
    }

    private void initView() {
        list = new ArrayList<>();
        pagerAdapter = new NewsPagerAdapter(BaseNewsFragment.this.getChildFragmentManager(), list);
        binding.pagerNewsHome.setAdapter(pagerAdapter);
        binding.imageTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingOpen.isOpenOrClose();
            }
        });
    }

    @Override
    public void onResponse(Call<NewsTagEntity> call, Response<NewsTagEntity> response) {
        if (response.body()!=null) {
            list.addAll(response.body().getData().getChannels());
            pagerAdapter.notifyDataSetChanged();
            binding.tabLayoutNews.setupWithViewPager(binding.pagerNewsHome);

        }
    }

    @Override
    public void onFailure(Call<NewsTagEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        slidingOpen = null;
    }
}
